package com.rain.builder;


import com.rain.utils.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class BuildBase {

    private static Logger logger = LoggerFactory.getLogger(BuildBase.class);

    public static void execute() throws IOException {
        List<String> headerInfoList = new ArrayList<>();

        // 生成枚举类
        headerInfoList.add("package " + Constants.PACKAGE_ENUM);
        build(headerInfoList,"DateTimePatternEnum", Constants.PACKAGE_ENUM_PATH);

        // 生成日期工具类
        headerInfoList.clear();
        headerInfoList.add("package " + Constants.PACKAGE_UTILS);
        build(headerInfoList,"DateUtil", Constants.PACKAGE_UTILS_PATH);

        // 生成baseMapper
        headerInfoList.clear();
        headerInfoList.add("package " + Constants.PACKAGE_MAPPER);
        build(headerInfoList,"BaseMapper", Constants.PACKAGE_MAPPER_PATH);

        // 生成pageSize枚举
        headerInfoList.clear();
        headerInfoList.add("package " + Constants.PACKAGE_ENUM);
        build(headerInfoList,"PageSize", Constants.PACKAGE_ENUM_PATH);

        // 生成SimplePage
        headerInfoList.clear();
        headerInfoList.add("package " + Constants.PACKAGE_QUERY);
        headerInfoList.add("import " + Constants.PACKAGE_ENUM + ".PageSize");
        build(headerInfoList,"SimplePage", Constants.PACKAGE_QUERY_PATH);

        // 生成BaseQuery
        headerInfoList.clear();
        headerInfoList.add("package " + Constants.PACKAGE_QUERY);
        build(headerInfoList,"BaseQuery", Constants.PACKAGE_QUERY_PATH);

        // 生成PaginationResult
        headerInfoList.clear();
        headerInfoList.add("package " + Constants.PACKAGE_QUERY);
        build(headerInfoList,"PaginationResult", Constants.PACKAGE_QUERY_PATH);

        // 生成BusinessException
        headerInfoList.clear();
        headerInfoList.add("package " + Constants.PACKAGE_EXCEPTION);
        headerInfoList.add("import " + Constants.PACKAGE_ENUM + ".ResponseCodeEnum");
        build(headerInfoList,"BusinessException", Constants.PACKAGE_EXCEPTION_PATH);

        // 生成ResponseCodeEnum
        headerInfoList.clear();
        headerInfoList.add("package " + Constants.PACKAGE_ENUM);
        build(headerInfoList,"ResponseCodeEnum", Constants.PACKAGE_ENUM_PATH);

        // 生成ResponseVO
        headerInfoList.clear();
        headerInfoList.add("package " + Constants.PACKAGE_QUERY);
        build(headerInfoList,"ResponseVO", Constants.PACKAGE_QUERY_PATH);

        // 生成BaseController
        headerInfoList.clear();
        headerInfoList.add("package " + Constants.PACKAGE_CONTROLLER);
        headerInfoList.add("import " + Constants.PACKAGE_ENUM + ".ResponseCodeEnum");
        headerInfoList.add("import " + Constants.PACKAGE_QUERY + ".ResponseVO");
        build(headerInfoList,"BaseController", Constants.PACKAGE_CONTROLLER_PATH);

        // 生成AGlobalExceptionHandlerController
        headerInfoList.clear();
        headerInfoList.add("package " + Constants.PACKAGE_CONTROLLER);
        headerInfoList.add("import " + Constants.PACKAGE_ENUM + ".ResponseCodeEnum");
        headerInfoList.add("import " + Constants.PACKAGE_QUERY + ".ResponseVO");
        headerInfoList.add("import " + Constants.PACKAGE_EXCEPTION + ".BusinessException");
        headerInfoList.add("import lombok.extern.slf4j.Slf4j;");
        build(headerInfoList,"AGlobalExceptionHandlerController", Constants.PACKAGE_CONTROLLER_PATH);
    }

    public static void build(List<String> headerInfoList, String fileName, String outPutPath) throws IOException {
        File folder = new File(outPutPath);
        if (!folder.exists()) {
            folder.mkdirs();
        }

        File file = new File(outPutPath, fileName + ".java");

        OutputStream outputStream = null;
        OutputStreamWriter outputStreamWriter = null;
        BufferedWriter bufferedWriter = null;

        InputStream inputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader bufferedReader = null;

        try {
            outputStream = new FileOutputStream(file);
            outputStreamWriter = new OutputStreamWriter(outputStream,"UTF-8");
            bufferedWriter = new BufferedWriter(outputStreamWriter);

            String templatePath = BuildBase.class.getClassLoader().getResource("template/" + fileName + ".txt").getPath();
            inputStream = new FileInputStream(templatePath);
            inputStreamReader = new InputStreamReader(inputStream,"UTF-8");
            bufferedReader = new BufferedReader(inputStreamReader);

            for (String head: headerInfoList){
                bufferedWriter.write(head + ";");
                bufferedWriter.newLine();
                if(head.contains("package") || head.contains("import")){
                    bufferedWriter.newLine();
                }

            }

            String lineInfo = null;
            while((lineInfo = bufferedReader.readLine()) != null){
                bufferedWriter.write(lineInfo);
                bufferedWriter.newLine();
            }
            bufferedWriter.flush();



        }catch (Exception e){
            logger.error("生成基础类: {} 失败",fileName,e.getMessage());
            e.printStackTrace();
        }finally {
            if(inputStream != null){
                inputStream.close();
            }
            if(outputStream != null){
                outputStream.close();
            }
            if(inputStreamReader != null){
                inputStreamReader.close();
            }
            if(outputStreamWriter != null){
                outputStreamWriter.close();
            }
            if(bufferedWriter != null){
                bufferedWriter.close();
            }
            if(bufferedReader != null){
                bufferedReader.close();
            }
        }
    }

}
